package com.xq.springboot.controller.handler;

import com.xq.springboot.domain.ResponseResult;
import com.xq.springboot.exception.GirlException;
import com.xq.springboot.util.HttpResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 抛出到controller层的异常处理类.
 *
 * @author 郭凯强
 */
@ControllerAdvice
public class ControllerExceptionHandler {
	private final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);

	@ExceptionHandler
	@ResponseBody
	public ResponseResult handleException(Exception e) {
		if (e instanceof GirlException) {
			GirlException girlException = (GirlException) e;
			return HttpResultUtil.error(girlException.getErrorCode(), girlException.getMessage());
		} else {
			logger.error("[系统异常]{}", e);
			return HttpResultUtil.error(-1, "未知错误!");
		}
	}
}
